$(function () {
    var token = sessionStorage.getItem('token');
    var userName = sessionStorage.getItem('userName');
    $('#userName1').html(userName);

    $('#resetPass').click(function () {
        $('#myModal').modal('show')
        $('#setPass').click(function () {
            var oldPass = $('#oldPass').val()
            var newPass = $('#newPass').val()
            var newPassTwo = $('#newPassTwo').val()
            if (oldPass.replace(/\s+/g, "") == ""){
                $.message({
                    message:'请输入旧密码',
                    type:'warning'
                });
                return;
            }
            if (newPass.replace(/\s+/g, "") == ""){
                $.message({
                    message:'请输入新密码',
                    type:'warning'
                });
                return;
            }
            if (newPassTwo.replace(/\s+/g, "") == ""){
                $.message({
                    message:'请输入确认密码',
                    type:'warning'
                });
                return;
            }
            if (oldPass != sessionStorage.getItem("userPwd")){
                $.message({
                    message:'旧密码不正确',
                    type:'warning'
                });
                return;
            }
            if (newPass != newPassTwo){
                $.message({
                    message:'新密码和确认密码不一致',
                    type:'warning'
                });
                return;
            }
            $.ajax({
                url: '/oa/t-user/setPass',
                data:{userId:sessionStorage.getItem("id"),userPwd:newPass},
                type: "POST",
                dataType: "json",
                headers: {
                    token: "" + token
                },
                success: function (data) {
                    if (data.code == 200){
                        $.message(data.msg);
                        window.location.href = 'login.html'
                    }else if (data.code == 401) {
                        window.location.href = 'login.html'
                    }else {
                        $.message({
                            message:data.msg,
                            type:'error'
                        });
                    }
                }
            })
        })
    });

    $('#logout').click(function () {
        $.ajax({
            url: '/oa/t-user/logout?id='+sessionStorage.getItem("id"),
            type: "GET",
            dataType: "json",
            success: function (data) {
                window.location.href = 'login.html'
            }
        })
    });
});